using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using BaseForum.Model;

namespace BaseForum.Mockup
{
    public class WebForumMockup
    {
        public static List<User> users = new List<User>();
        public static List<Doctor> doctors = new List<Doctor>();
        public static List<Forum> forums = new List<Forum>();
        public static List<Topic> topics = new List<Topic>();
        public WebForumMockup()
        {
            GenerateMockup();
        }
        public static string HashMD5(string input)
        {
            MD5 md5 = MD5.Create();
            byte[] inputBytes = Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
        public static void GenerateMockup()
        {
            //Mockup normal user & doctor user
            for (int i = 1; i < 10; i++)
            {
                User user = new User();
                user.UserName = i < 5 ? "User" + i : "Doctor" + i;
                user.Name = i < 5 ? "User" + i : "Doctor" + i;
                user.Type = i < 5 ? User.UserNormal : User.UserDoctor;
                user.Password = i < 5 ? HashMD5("User" + i) : HashMD5("Doctor" + i);
                user.Age = 24 + i;
                users.Add(user);
            }//More info for doctor
            foreach (User user in users)
            {
                if (user.Type == User.UserDoctor)
                {
                    Doctor doctor = new Doctor(user);                   
                    doctor.Workplace = "Workplace" + user.Id;
                    doctor.Specialization = "Specialization" + user.Id;
                    doctor.Experience = "Experience" + user.Id;
                    doctor.Achievement = "Achievement" + user.Id;
                    doctors.Add(doctor);
                }
            }//Admin user
            User admin = new User();
            admin.UserName = "admin";
            admin.Name = "admin";
            admin.Type = User.UserAdmin;
            admin.Password = HashMD5("admin");
            admin.Age = 24;
            users.Add(admin);
            //Forum ,SubForum and Topic
            for (int i = 1; i < 40; i++)
            {
                int topicId = 1;
                int forumID = 1;
                //Create Forum
                Forum forum = new Forum();
                forum.Id = forumID++;
                forum.Title = "Forum " + i;
                forum.Desc = "Desc " + i;
                forum.ParrentForum = null;
                forums.Add(forum);
                //Create Sub Forum
                for (int j = i + 1; j < i + 10; j++)
                {
                    Forum f = new Forum();
                    f.Id = forumID++;
                    f.Title = "Sub Forum " + j + " of Forum " + i;
                    f.Desc = "Desc of sub forum " + j;
                    f.ParrentForum = forum;
                    forums.Add(f);
                    //Create Topic in this forum
                    for (int k = 1; k < 15; k++)
                    {
                        Topic topic = new Topic();
                        topic.Id = topicId++;
                        topic.Forum = f;
                        topic.Title = "Topic Title" + topic.Id;
                        topic.User = users[1];
                        topic.Content = "Content" + topic.Id;
                        topics.Add(topic);
                        //Create sub topic in this forum
                        for(int l = 1;l<10;l++)
                        {
                            Topic t = new Topic();
                            t.Id = topicId++;
                            //t.Forum = f;
                            t.ParrentTopic = topic;
                            t.Title = "Sub Topic Title" + t.Id;
                            t.User = users[2];
                            t.Content = "Sub Topic Content" + t.Id;
                            topics.Add(t);
                        }
                    }
                }
                i += 9;
            }
        }

        public static Topic GetTopic(int id)
        {
            foreach (Topic topic in topics)
            {
                if (topic.Id == id)
                {
                    return topic;
                }
            }
            return null;
        }
    }

}
